package com.gxuwz.medical.domain.chronicSyndrome;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gxuwz.medical.tools.DateUtil;
import com.gxuwz.medical.domain.chronicdisApply.ChronicdisApply;
import com.gxuwz.medical.domain.member.*;
import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.exception.DbException;

/**
 * 慢性病证管理
 * 
 * @author Administrator
 * 
 */
public class ChronicSyndrome {

	private Member member; // 农合证号
	private String mxbzcode; // 慢性病证号=农合证号(14位)+建档时间（14位）
	private String idcard; // 身份证号
	private String name; // 持证者姓名
	private String chronicname; // 慢性病名称
	private String startTime; // 开始时间
	private String endTime; // 结束时间
	private String status; // 状态:1表示正常、0表示注销
	private java.util.Date creattime; // 建档时间
	private String creator; // 建档人

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getMxbzcode() {
		return mxbzcode;
	}

	public void setMxbzcode(String mxbzcode) {
		this.mxbzcode = mxbzcode;
	}

	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getChronicname() {
		return chronicname;
	}

	public void setChronicname(String chronicname) {
		this.chronicname = chronicname;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public java.util.Date getCreattime() {
		return creattime;
	}

	public void setCreattime(java.util.Date creattime) {
		this.creattime = creattime;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public ChronicSyndrome(Member member, String mxbzcode, String idcard,
			String name, String chronicname, String startTime, String endTime,
			String status, Date creattime, String creator) {
		super();
		this.member = member;
		this.mxbzcode = mxbzcode;
		this.idcard = idcard;
		this.name = name;
		this.chronicname = chronicname;
		this.startTime = startTime;
		this.endTime = endTime;
		this.status = status;
		this.creattime = creattime;
		this.creator = creator;
	}

	public ChronicSyndrome() {
	}

	/*
	 * public static List<ChronicSyndrome> getNewsList(String where) throws
	 * SQLException { Connection conn = null; PreparedStatement pstmt = null; //
	 * System.out.println("where:" + where);
	 * 
	 * List<ChronicSyndrome> newslist = new ArrayList<ChronicSyndrome>();
	 * 
	 * conn = DbUtil.getConn();
	 * 
	 * String sql = "select * from t_chronic_syndrome where " + where; pstmt =
	 * conn.prepareStatement(sql); // System.out.println("sql:" + sql);
	 * ResultSet rs = pstmt.executeQuery(sql); try { while (rs.next()) {
	 * ChronicSyndrome news = new ChronicSyndrome(); String nhzcode =
	 * rs.getString("nhzcode"); String mxbzcode = rs.getString("mxbzcode");
	 * String idcard = rs.getString("idcard"); String chronicname =
	 * rs.getString("chronicname"); String startTime =
	 * rs.getString("startTime"); String endTime = rs.getString("endTime");
	 * String name = rs.getString("name"); Date creattime =
	 * rs.getDate("creattime"); String creator = rs.getString("creator");
	 * 
	 * news.setNhzcode(nhzcode); news.setMxbzcode(mxbzcode);
	 * news.setIdcard(idcard); news.setChronicname(chronicname);
	 * news.setStartTime(startTime); news.setEndTime(endTime);
	 * news.setName(name); news.setCreattime(creattime);
	 * news.setCreator(creator);
	 * 
	 * newslist.add(news); } rs.close(); pstmt.close(); } catch (SQLException e)
	 * { e.printStackTrace(); } finally { conn.close(); } return newslist; }
	 */
	public void addChronicSyndrome() throws Exception {
		Connection conn = null;
		try {
			Date creattime = new Date();
			String no = DateUtil
					.format(creattime, DateUtil.DFMT_YYYYMMDDHHMMSS);
			this.creattime = creattime;
			// 慢病档案编号
			this.mxbzcode = this.idcard + no;
			// 1:获取连接实例
			conn = DbUtil.getConn();
			// 2:持久化数据
			saveToDB(conn);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			// 关闭连接
			DbUtil.close(conn);
		}
	}

	/**
	 * 持久化数据
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"INSERT INTO t_chronic_syndrome (nhzcode,mxbzcode, idcard, chronicname, startTime,endTime,name,status,creattime,creator)");
			sqlBuff.append("VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			int index = 1;
			System.out.println(this.getMember().getNhzcode());
			pstmt.setString(index++, this.getMember().getNhzcode());
			pstmt.setString(index++, this.mxbzcode);
			pstmt.setString(index++, this.idcard);
			pstmt.setString(index++, this.chronicname);
			pstmt.setString(index++, this.startTime);
			pstmt.setString(index++, this.endTime);
			pstmt.setString(index++, this.name);
			pstmt.setString(index++, this.status);
			pstmt.setDate(index++, new java.sql.Date(this.creattime.getTime()));
			pstmt.setString(index++, this.creator);
			int count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DbUtil.close(pstmt);
		}
	}
	
	public void del() throws Exception {
		Connection conn = null;
		try {
			conn = DbUtil.getConn();
			// 持久化数据
			delForDB(conn);
		}catch(Exception e){
			e.getStackTrace();
		}finally {
			// 关闭连接
			DbUtil.close(conn);
		}
	}
	
	protected void delForDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuffer = new StringBuffer();
			sqlBuffer.append("delete from t_chronic_syndrome where mxbzcode=?");
			pstmt = conn.prepareStatement(sqlBuffer.toString());
			pstmt.setString(1, this.mxbzcode);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("删除t_chronic_syndrome时出错！",e);
		} finally {
			DbUtil.close(pstmt);
		}
	}
	
	
	

}
